package com.app.signing.view.setting

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import com.app.signing.R
import com.app.signing.data.SpManager
import com.app.signing.view.base.BaseActivity
import com.app.signing.view.login.LoginActivity
import com.app.signing.view.login.LoginStates
import com.app.signing.view.setting.export.ExportExcel
import com.app.signing.view.setting.signing.SigningToolActivity
import com.app.signing.widget.ExportNameDialog
import com.app.signing.widget.SigningIntervalDialog
import com.app.signing.widget.SigningSettingDialog.NoticeDialogListener
import com.app.signing.widget.StudyTimeDialog
import kotlinx.android.synthetic.main.activity_setting.*

class SettingActivity : BaseActivity(), NoticeDialogListener {

    override fun getLayoutId(): Int {
        return R.layout.activity_setting
    }

    override fun initData() {
    }

    override fun initView(savedInstanceState: Bundle?) {
        setSupportActionBar(toolbar_setting)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
        if (SpManager.INSTANCE.readLoginStates() == LoginStates.loginAdmin) {
            ll_setting.visibility = View.VISIBLE
        } else {
            ll_setting.visibility = View.GONE
        }
        text_signing_range_setting.setOnClickListener {
            startActivity(Intent(this, SigningToolActivity::class.java))
        }
        text_signing_export.setOnClickListener {
            ExportNameDialog.newInstance().show(supportFragmentManager, "export_name")
        }
        bt_logout.setOnClickListener {
            SpManager.INSTANCE.deleteLoginUserId()
            SpManager.INSTANCE.deleteLoginStates()
            val intent = Intent(this, LoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            startActivity(intent)
        }
        text_signing_interval.setOnClickListener {
            SigningIntervalDialog.newInstance().show(supportFragmentManager, "signing_interval")
        }
        text_study_time.setOnClickListener {
            StudyTimeDialog.newInstance().show(supportFragmentManager, "study_time")
        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home -> {
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onDialogPositiveClick(
            dialog: DialogFragment?,
            value: String?
    ) {
        dialog?.let {
            when (it) {
                is ExportNameDialog -> value?.let {
                    ExportExcel.export(it)
                }
                is SigningIntervalDialog -> {
                    value?.let {
                        if (TextUtils.isEmpty(it)) {
                            return
                        }
                        if (it.toInt() <= 0) {
                            return
                        }
                        SpManager.INSTANCE.saveSigningInterval(it)
                    }
                }
                is StudyTimeDialog -> value?.let {
                    if (TextUtils.isEmpty(it)) {
                        return
                    }
                    if (it.toInt() <= 0) {
                        return
                    }
                    SpManager.INSTANCE.saveStudyTime(it)
                }
                else -> {

                }
            }
        }
    }

    override fun onDialogNegativeClick(dialog: DialogFragment?) {
    }
}
